package com.qf.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.annotation.RequiresPermissionsDesc;
import com.qf.pojo.DtsComment;
import com.qf.pojo.DtsOrder;
import com.qf.pojo.DtsOrderGoods;
import com.qf.pojo.DtsUser;
import com.qf.service.DtsCommentService;
import com.qf.service.DtsOrderGoodsService;
import com.qf.service.DtsOrderService;
import com.qf.service.DtsUserService;
import com.qf.util.ResponseUtil;
import com.qf.vo.CatVo;
import com.qf.vo.ShipVo;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

/**
 * @author 邢式瑞
 * @version 1.0
 * @project E-commercebackGroundManagementSystem
 * @date 2022/9/24 11:34
 */
@RestController
@RequestMapping("/admin/order")
@Slf4j
@CrossOrigin
public class OrderController {
    @Autowired
    private DtsOrderService dtsOrderService;
    @Autowired
    private DtsOrderGoodsService dtsOrderGoodsService;
    @Autowired
    private DtsUserService dtsUserService;
    @Autowired
    private DtsCommentService dtsCommentService;

    //http://localhost:8080/admin/order/list?page=1&limit=20&orderStatusArray=102&sort=add_time&order=desc&userId=s&orderSn=d
    @RequiresPermissionsDesc(menu = {"商场管理","订单管理"},button ="查找")
    @RequiresPermissions("admin:order:list")
@GetMapping("/list")
    public  Object orderList(Integer page ,Integer limit,Integer[] orderStatusArray,String sort,String order,String orderSn){
   Page<DtsOrder> dtsOrderPage= dtsOrderService.selectPage(page,limit,order,sort,orderStatusArray,orderSn);
    long total = dtsOrderPage.getTotal();
    List<DtsOrder> records = dtsOrderPage.getRecords();

    HashMap<String, Object> data = new HashMap<>();
    data.put("total",total);
    data.put("items",records);

    return ResponseUtil.ok(data);
}
//http://localhost:8080/admin/order/listShipChannel
    @GetMapping("/listShipChannel")
    public  Object listShipChannel(){
        Set<ShipVo> catVos= dtsOrderService.selectlistShipChannel();
        HashMap map = new HashMap();
        map.put("shipChannelList",catVos);
        return ResponseUtil.ok(map);

    }
    //http://localhost:8080/admin/order/ship
    @PostMapping("ship")
    public  Object ShipOrder(@RequestBody DtsOrder dtsOrder){
    dtsOrder.setUpdateTime(new Date());
      dtsOrderService.updateOrder(dtsOrder);
      return ResponseUtil.ok(dtsOrder);
    }
    //http://localhost:8080/admin/order/detail?id=6
    /*
    @GetMapping("/detail")
public Object detail(Integer id){
    List<DtsOrderGoods> orderGoods = dtsOrderGoodsService.findByOrderId(id);
    DtsOrder dtsOrder = dtsOrderService.findOrderById(id);
    UserVo userVo = dtsUserService.findUserVoById(dtsOrder.getUserId());
    Map data = new HashMap();
    data.put("orderGoods",orderGoods);
    data.put("user", userVo);
    data.put("order",dtsOrder);
    return ResponseUtil.ok(data);

     */
    @GetMapping("/detail")
    public Object OrderList(Integer id){
    DtsOrder dtsOrder =  dtsOrderService.selectById(id);
        List<DtsOrderGoods> dtsOrderGoods=   dtsOrderGoodsService.selectByOrderId(id);
        DtsUser dtsUser = dtsUserService.selectUserById(dtsOrder.getUserId());
        HashMap hashMap = new HashMap();
        hashMap.put("orderGoods",dtsOrderGoods);
        hashMap.put("user",dtsUser);
        hashMap.put("order",dtsOrder);

        return  ResponseUtil.ok(hashMap);
    }
    //T http://localhost:8080/admin/issue/list?page=1&limit=20&question=ss&sort=add_time&order=des
  //  http://localhost:8080/admin/order/reply
    @RequiresPermissionsDesc(menu = {"商场管理","商品评论"},button ="回复")
    @RequiresPermissions("admin:order:reply")
    @PostMapping("/reply")
    public  Object commonList(@RequestBody DtsComment dtsComment){
        dtsComment.setAddTime(new Date());
        dtsComment.setUpdateTime(new Date());
        dtsComment.setDeleted(false);
        dtsCommentService.insert(dtsComment);
        return ResponseUtil.ok(dtsComment);
    }

}
